package com.mingqijia.gassafety.console.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.db.hangzhou.entity.AlertRecordSign;
import com.mingqijia.gassafety.shared.constant.EquipmentType;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.*;
import com.mingqijia.gassafety.webserver.response.*;
import com.mingqijia.gassafety.webserver.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.ws.Holder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 记录查询
 *
 * @author guoliang.lao
 * @since 2022-02-28
 */

@Slf4j
@Api(tags = "记录查询")
@RestController(value = "RecordLogController")
@RequestMapping("/business/v1/record/log")
public class RecordLogController {

    @Autowired
    AlertLogService alertLogService;
    @Autowired
    HistoryBrokenService historyBrokenService;
    @Autowired
    HistoryAlarmService historyAlarmService;
    @Autowired
    PowerLogService powerLogService;
    @Autowired
    ResetlogService resetlogService;
    @Autowired
    MaintainlogService maintainlogService;
    @Autowired
    AlertRecordSignService alertRecordSignService;
    @Autowired
    ResetDetailService resetDetailService;
    @Autowired
    ConfigureHolder holder;

    @ApiOperation("报警记录")
    @RequestMapping(value = "alert", method = RequestMethod.POST)
    public CommonResponse<PageUtil<AlertLogResponse>> alert(@RequestBody RecordLogRequest req){
        if (!StringUtils.isEmpty(req.getEquipmentType()) && EquipmentType.GAS_ALARM_CONTROLLER.getCode().equals(req.getEquipmentType())) {
            return CommonResponse.failure("设备类型不正确");
        }
        return CommonResponse.success(historyAlarmService.getAlarmList(req));
    }


    @ApiOperation("故障记录")
    @RequestMapping(value = "broken", method = RequestMethod.POST)
    public CommonResponse<PageUtil<BrokenLogResponse>> broken(@RequestBody RecordLogRequest req){
        return CommonResponse.success(historyBrokenService.getList(req));
    }

    @ApiOperation("复位记录")
    @RequestMapping(value = "reset", method = RequestMethod.POST)
    public CommonResponse<PageUtil<ResetLogResponse>> reset(@RequestBody RecordLogRequest req){
        if (!StringUtils.isEmpty(req.getEquipmentType()) && EquipmentType.GAS_ALARM_CONTROLLER.getCode().equals(req.getEquipmentType())) {
            return CommonResponse.failure("设备类型不正确");
        }
        return CommonResponse.success(resetlogService.resetList(req));
    }

    @ApiOperation("开关机记录")
    @RequestMapping(value = "power", method = RequestMethod.POST)
    public CommonResponse<PageUtil<PowerLogResponse>> power(@RequestBody PowerLogRequest req){
        log.info("powerr start:"+ JSONObject.toJSONString(req));
        if (!StringUtils.isEmpty(req.getEquipmentType()) && EquipmentType.POINT_TYPE_GAS_DETECTOR.getCode().equals(req.getEquipmentType())) {
            return CommonResponse.failure("设备类型不正确");
        }
        return CommonResponse.success(powerLogService.list(req));
    }

    @ApiOperation("维护记录")
    @RequestMapping(value = "maintain", method = RequestMethod.POST)
    public CommonResponse<PageUtil<MaintainLogResponse>> maintain(@RequestBody MaintainLogRequest req){
        return CommonResponse.success(maintainlogService.maintainList(req));
    }

    @ApiOperation("复位记录-处理详情")
    @PostMapping(value = "reset/handleList")
    public CommonResponse<PageUtil<ResetHandlerResponse>> resetHandleList(@RequestBody @Valid ResetHandlerRequest request  ){
        return CommonResponse.success(resetDetailService.detailList(request));

    }

    @ApiOperation("复位记录-处理详情 删除")
    @GetMapping(value = "reset/handleDelete")
    public CommonResponse<Object> resetHandleDelete (@RequestParam @ApiParam("复位处理详情id") String detailId){
        resetDetailService.delete(detailId);
        return CommonResponse.success("");
    }

    @ApiOperation("复位记录-新增处理记录")
    @PostMapping(value = "reset/handleInsert")
    public CommonResponse<Object> resetHandleInsert (@RequestBody @Valid ResetInsertRequest request ){
        resetDetailService.insert(request);
        return CommonResponse.success("");
    }
    @ApiOperation("报警记录处理标记")
    @RequestMapping(value = "alert/sign", method = RequestMethod.POST)
    public CommonResponse<Object> sign( @RequestBody  @Valid SignRequest req){
        alertRecordSignService.sign(req);
        return CommonResponse.success(null,"success");
    }
    @ApiOperation("报警记录处理查询")
    @RequestMapping(value = "alert/query", method = RequestMethod.POST)
    public CommonResponse<PageUtil<AlertRecordSign>> query(@RequestBody AlertRecordSignRequest req ){
        return CommonResponse.success(alertRecordSignService.query(req),"success");
    }

    @ApiOperation("报警处理情况")
    @RequestMapping(value = "alert/condition", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getAlertConditionList (@RequestBody JSONObject jsonObject){
        Integer isSpecialized = jsonObject.getInteger("isSpecialized");
        return CommonResponse.success(historyAlarmService.getConditionList(isSpecialized),"success");
    }

    @ApiOperation("故障处理情况")
    @RequestMapping(value = "broken/condition", method = RequestMethod.POST)
    public CommonResponse<Map<String, Integer>> getBrokenConditionList (){
        return CommonResponse.success(historyBrokenService.getConditionList(),"success");
    }

    @ApiOperation("处理标记历史数据同步")
    @RequestMapping(value = "alert/signSync", method = RequestMethod.POST)
    public CommonResponse<Object> signSync( ){
        alertRecordSignService.signSync();
        return CommonResponse.success(null,"success");
    }




}

